<?php
class model__module_model extends database_mysql
{
    public function __construct()
    {
        parent::__construct();
        
    }
    
    public function md_cm_get_lang(){
        
        $select = " 
                    SELECT
                    ". prefix ."language.ID,
                    ". prefix ."language.Lang,
                    ". prefix ."language.DirName,
                    ". prefix ."language.`Code`
                    FROM
                    ". prefix ."language
                    WHERE
                    ". prefix ."language.`Status` = 'true'
        ";
        return parent::mysql_fetch_all_rows($select);
    }
    
    public function md_cm_insert_module($module_name, $app){
        if($app == 'admin'){
            $app_id = '1';
        }else{
            $app_id = '2';
        }
        $sql = "
                    SELECT
                    ". prefix ."modules.`Name`
                    FROM
                    ". prefix ."modules
                    WHERE
                    ". prefix ."modules.`Name` = '$module_name'

        ";
        $result = parent::mysql_one_row($sql);
        if(empty($result)){
            $data = array(
                'Name' => $module_name,
                'AppID' => $app_id,
                'Status' => 'true',
            );
            $id_last = $this->inser_db('modules', $data);
            $list_lang = $this->md_cm_get_lang();
            if(!empty($list_lang) && $id_last >0){
                foreach($list_lang as $value){
                    $data = array();
                    $data['ModuleTitle'] = $module_name;
                    $data['ModuleID'] = $id_last;
                    $data['Status'] = 'true';
                    $data['LangID'] = $value['ID'];
                    $result = $this->inser_db('modules_lang', $data);
                }
            }
        }
        
    }

    public function inser_db($table, $data){
        $insKey = array();
        $insVal = array();
        foreach($data as $k => $v){
            $v = parent::mysql_check_string($v);
            array_push($insKey,"`".$k."`");
            array_push($insVal,"'".$v."'");
        }
        //echo "INSERT INTO ". prefix ."$table (".implode(',',$insKey).") VALUES (".implode(',',$insVal).")". '<BR>';
        $this->mysql_update_insert("INSERT INTO ". prefix ."$table (".implode(',',$insKey).") VALUES (".implode(',',$insVal).")");
        $id_last =  mysql_insert_id();
        return $id_last;
    }
    
}
